import Vue from 'vue'
import VueRouter from 'vue-router'
import MyLogin from '@/components/MyLogin'
import MyHome from '@/components/MyHome'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: MyLogin },
    {
      path: '/home',
      component: MyHome,
      children: [
        { path: 'goods', component: () => import('@/components/menus/MyGoods.vue') },
        { path: 'order', component: () => import('@/components/menus/MyOrders.vue') },
        { path: 'right', component: () => import('@/components/menus/MyRights.vue') },
        { path: 'settin', component: () => import('@/components/menus/MySettings.vue') },
        { path: 'users', component: () => import('@/components/menus/MyUsers.vue') },
        { path: 'userinfo/:id', component: () => import('@/components/user/MyUserDetail.vue') }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  // 判断是否等于首页login
  if (to.path === '/login') {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      this.$router.push('/login')
    }
  }
})
export default router
